package com.xsm.activitygroup;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.xsm.activitygroup.R;
import com.xsm.internet.PostMessage;
import com.xsm.internet.ConnectUrl;
import com.xsm.view.LoadDialog;
import com.xsm.view.Mydialog;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class About extends Activity{
	Activity context;
	TextView codeText;
	String packageName="",versionname="";
	String versioncode;
	 DownloadManager manager ;  
  SharedPreferences sharedPreferences;
  LoadDialog loadDialog;
  Button update;
protected void onCreate(Bundle savedInstanceState) {
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.about);
	context=this;
	 manager =(DownloadManager)getSystemService(DOWNLOAD_SERVICE);  
  
     codeText=(TextView)findViewById(R.id.codeText);
     update=(Button)findViewById(R.id.update);
     
	loadDialog=new LoadDialog(context,"正在检测更新...");
	sharedPreferences=getSharedPreferences("XSM", 0);
packageName=sharedPreferences.getString("packageName", "");
versioncode=sharedPreferences.getString("versioncode","");
versionname=sharedPreferences.getString("versionname", "");
codeText.setText("书集 V"+versionname);
}
public void Back(View view){
	finish();
}
public void Update(View view){
	update.setEnabled(false);
	loadDialog.showDialog();
	new Update().execute();
}


public class Update extends AsyncTask<Void, Void, String>{
	protected String doInBackground(Void... arg0) {
   List params=new ArrayList();
		params.add(new BasicNameValuePair("ASK","Update"));
		params.add(new BasicNameValuePair("packageName",packageName));
		params.add(new BasicNameValuePair("versioncode",versioncode));
		PostMessage postMessage=new PostMessage(params);
		String result=postMessage.getResult();
		return result;
	}
	protected void onPostExecute(final String result) {
		if(result.equals("-1"))
		{Toast.makeText(context, "连接服务器失败,请检查网络配置", Toast.LENGTH_LONG).show();
		loadDialog.dissmissDialog();
		 update.setEnabled(true);
		return ;
		}
		if(result.equals("[]")){
		    	Toast.makeText(context, "暂无最新版本", Toast.LENGTH_LONG).show();	    
		    	loadDialog.dissmissDialog();
				 update.setEnabled(true);
				 return;
		}
		JSONArray array = null;
		String text="";
		try {
			array = new JSONArray(result);
			JSONObject versioncode=array.getJSONObject(2);
			String vcode=versioncode.getString("versioncode");
			String ncode=sharedPreferences.getString("versioncode","0");
		    if(Integer.parseInt(vcode)>Integer.parseInt(ncode)){
				JSONObject updatetext=array.getJSONObject(0);
				text=updatetext.getString("updatetext");
		final Mydialog dialog=new Mydialog(context,filteLineBreak(text));	
		dialog.setEnsureText("更新");
		dialog.settitle("发现新版本");
	    dialog.setEnsurelistener(new OnClickListener() {
		public void onClick(View v) {
			Intent intent=new Intent();
			intent.setClass(context,UpdateActivity.class);
			intent.putExtra("updatetext", result);
			startActivity(intent);
		dialog.dissmissDialog();
		}
	});
		dialog.showDialog();
		    }
		} catch (JSONException e) {
	Log.d("my", e+"");
		}	
		
		 if(loadDialog!=null)
			 loadDialog.dissmissDialog();
			 update.setEnabled(true);
 
	super.onPostExecute(result);
	}
	
	
}



public static String filteLineBreak(String text)
{   String tstring=""; 
	String s[]=text.split("-");
	if(s.length>1){
    for(int i=1;i<s.length;i++){
    	tstring=tstring+"-"+s[i]+"\n";
    }
    return tstring;
	}else {
		return "";
	}
}
  
}
